﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;

namespace UpDryTwistImageReader {

    /// <summary>
    /// Greg Tatham comments:
    /// I adapted this substantially from Andy Wilson's code: http://www.csharp-examples.net/get-files-from-directory/
    /// </summary>
    class ImageList : List<ImageFileAttributes> {

        #region Constructor

        public ImageList(Configuration config, string folder, List<string> extensions, bool immediateScan = true) {

            m_config = config;
            m_folder = folder;
            m_extensions = extensions;
            if (immediateScan) {
                this.scan();
            }
        }

        #endregion

        #region Methods:  Scan()

        public void scan ( ) {

            m_config.log("Scanning folder " + m_folder);

            if (string.IsNullOrEmpty(m_folder)) return;

            List<string> sourceImages = new List<string>();

            foreach (string extension in m_extensions) {
                sourceImages.AddRange(Directory.GetFiles(m_folder, extension, SearchOption.AllDirectories));
                m_config.log(" ... now at " + sourceImages.Count.ToString() + " files after adding extension \"" + extension + "\" from folder \"" + m_folder + "\"");
            }

            List<int> removeIndex = new List<int>();

            // First, verify the cache.
            sourceImages.Sort(StringComparer.OrdinalIgnoreCase);

            for (int i = 0; i < this.Count; i++) {
                ImageFileAttributes imageAttributes = this[i];

                int sourceIndex = sourceImages.BinarySearch(imageAttributes.Path, StringComparer.OrdinalIgnoreCase);

                if (sourceIndex < 0) {
                    //image is not longer in main folder
                    removeIndex.Add(i);
                }
                else {
                    //item exists, remove it from the initial list
                    sourceImages.RemoveAt(sourceIndex);
                }
            }

            //we must remove from the end of the images list backwards otherwise we screw the index positions
            removeIndex.Reverse();

            for (int i = 0; i < removeIndex.Count; i++) {
                ImageFileAttributes imageAttributes = this[removeIndex[i]];

                // Remove image from cache
                this.RemoveAt(removeIndex[i]);
            }

            //now add new images into the cache
            for (int i = 0; i < sourceImages.Count; i++) {
                string imagePath = sourceImages[i];

                // Add image to cache
                Size originalSize = ImageHeader.GetDimensions(imagePath);
                this.Add(new ImageFileAttributes(imagePath, originalSize));
            }
        }

        #endregion

        #region Private Members

        private Configuration m_config;
        private string m_folder;
        private List<string> m_extensions;

        #endregion
    }
}
